/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Services;

import BackEnd.Country;
import BackEnd.State;
import BackEnd.Language;
import BackEnd.Linker;
import java.util.HashMap;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Gabi
 */
public class Common {
    private static final String urlS="http://eiffel.itba.edu.ar/hci/service/Common.groovy";

    public static void main(String[] args) {
        HashMap<String, Language> leng=GetLanguageList();
        int idEsp=leng.get("es").getId();
        HashMap<String, Country> coun=GetCountryList(idEsp);
        int idUsa=coun.get("USA").getId();
        HashMap<String, State> stat=GetStateList(idEsp, idUsa);
        for(State st: stat.values()) {
            System.out.println("id: "+st.getId()+" country_id: "+st.getCountryId()+ " name: "+st.getName()+" code: "+st.getCode());
        }
    }

    public static HashMap<String, Language> GetLanguageList() {
        
        HashMap<String, Language> map=null;
        
        try{
            Document doc=Linker.getData(urlS, "GetLanguageList", "GET", null);
            NodeList nl = doc.getElementsByTagName("language");
            map=new HashMap<String, Language>();
            for(int i=0; i<nl.getLength();i++) {
                Element e = (Element) nl.item(i);
                int id=Integer.parseInt(e.getAttribute("id"));
                NodeList nla = e.getElementsByTagName("code");
                String code=((CharacterData)((Element) nla.item(0)).getFirstChild()).getData();
                nla = e.getElementsByTagName("name");
                String name=((CharacterData)((Element) nla.item(0)).getFirstChild()).getData();
                map.put(code, new Language(id,name,code));
            }

        } catch (Exception e) {
            e.printStackTrace();
            //setErrorMessage(request, "Failed REST service call. Exception=" + e);
            //response = null;
        }
        return map;
    }

        public static HashMap<String, Country> GetCountryList(int language_id) {

        HashMap<String, Country> map=null;
        String[] args= {"language_id="+language_id};
        try{
            Document doc=Linker.getData(urlS, "GetCountryList", "GET", args);
            NodeList nl = doc.getElementsByTagName("country");
            map=new HashMap<String, Country>();
            for(int i=0; i<nl.getLength();i++) {
                Element e = (Element) nl.item(i);
                int id=Integer.parseInt(e.getAttribute("id"));
                NodeList nla = e.getElementsByTagName("code");
                String code=((CharacterData)((Element) nla.item(0)).getFirstChild()).getData();
                nla = e.getElementsByTagName("name");
                String name=((CharacterData)((Element) nla.item(0)).getFirstChild()).getData();
                map.put(code, new Country(id,name,code));
            }

        } catch (Exception e) {
            e.printStackTrace();
            //setErrorMessage(request, "Failed REST service call. Exception=" + e);
            //response = null;
        }
        return map;
    }

    public static HashMap<String, State> GetStateList(int language_id, int country_id) {
        
        HashMap<String, State> map=null;
        String[] args= {"language_id="+language_id , "country_id="+country_id};
        try{
            Document doc=Linker.getData(urlS, "GetStateList", "GET", args);
            NodeList nl = doc.getElementsByTagName("state");
            map=new HashMap<String, State>();
            for(int i=0; i<nl.getLength();i++) {
                Element e = (Element) nl.item(i);
                int id=Integer.parseInt(e.getAttribute("id"));
                NodeList nla = e.getElementsByTagName("code");
                String code=((CharacterData)((Element) nla.item(0)).getFirstChild()).getData();
                nla = e.getElementsByTagName("name");
                String name=((CharacterData)((Element) nla.item(0)).getFirstChild()).getData();
                nla = e.getElementsByTagName("country_id");
                int countryId=Integer.parseInt(((CharacterData)((Element) nla.item(0)).getFirstChild()).getData());
                map.put(code, new State(id,countryId,name,code));
            }

        } catch (Exception e) {
            e.printStackTrace();
            //setErrorMessage(request, "Failed REST service call. Exception=" + e);
            //response = null;
        }
        return map;
    }

}
